package com.example.coding_problem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import java.text.DecimalFormat;
import  java.text.NumberFormat;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class MyExtendableListViewAdapter extends BaseExpandableListAdapter {
    public String[] groupString = {"中医院站", "联想大厦站"};
    public String[][] childString = {
            {"1号", "2号"},
            {"1号", "2号"}
    };
    private List<Distance> distances=new ArrayList<>();
    public MyExtendableListViewAdapter(List<Distance> distances) {
        this.distances = distances;
    }
    @Override
    // 获取分组的个数
    public int getGroupCount() {
        return groupString.length;
    }

    //获取指定分组中的子选项的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return distances.get(groupPosition).getData().size();
    }

    //        获取指定的分组数据
    @Override
    public Object getGroup(int groupPosition) {
        return groupString[groupPosition];
    }

    //获取指定分组中的指定子选项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return distances.get(groupPosition).getData();
    }

    //获取指定分组的ID, 这个ID必须是唯一的
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子选项的ID, 这个ID必须是唯一的
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     */
// 获取显示指定分组的视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.open1, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_group_normal);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(groupString[groupPosition]);
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.open2, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.id=(TextView)convertView.findViewById(R.id.id);
            childViewHolder.number=convertView.findViewById(R.id.number);
            childViewHolder.distance = (TextView)convertView.findViewById(R.id.distance);
            childViewHolder.tv_distance=convertView.findViewById(R.id.tv_distance);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        //保留0位小数，且*1000
        DecimalFormat nf = new DecimalFormat("##0");
        DecimalFormat nf1 = new DecimalFormat("##0");
        childViewHolder.number.setText("("+distances.get(groupPosition).getData().get(childPosition).getPersons()+"人)");
        childViewHolder.id.setText(distances.get(groupPosition).getData().get(childPosition).getBus()+"号");
        childViewHolder.distance.setText("距离站台"+nf.format(distances.get(groupPosition).getData().get(childPosition).getDistance()*1000)+"米");
        childViewHolder.tv_distance.setText(nf1.format(distances.get(groupPosition).getData().get(childPosition).getDistance()*3)+"分钟到达");
        return convertView;
    }

    //指定位置上的子元素是否可选中

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvTitle;
    }

    public static class ChildViewHolder {
        public TextView distance;
        public TextView id;
        public TextView number;
        public TextView tv_distance;
    }
    public void updateData(List<Distance> distances){
        this.distances = distances;
    }
}
